import MockAdapter from 'axios-mock-adapter'
import axios from '@/api/index'   // 必须和页面用同一个实例

// 生成随机数据
const generateRandomData = (count, min, max) => {
  // 参数验证
  if (!Number.isInteger(count) || count < 0) {
    console.error('count必须是非负整数');
    return [];
  }
  if (typeof min !== 'number' || typeof max !== 'number') {
    console.error('min和max必须是数字');
    return [];
  }
  if (min > max) {
    console.error('min不能大于max');
    return [];
  }
  return Array.from({ length: count }, () => Math.floor(Math.random() * (max - min + 1)) + min);
};

export default function initMock() {

  console.log('initMock');

  const mock = new MockAdapter(axios, { delayResponse: 1500 })

  // POST /api/login
  // mock.onPost('/login').reply(config => {
  //   const { username, password } = JSON.parse(config.data)
  //   return username === 'admin' && password === '123456'
  //     ? [200, { token: 'fake-jwt' }]
  //     : [401, { msg: '账号或密码错误' }]
  // })

  // GET /api/test
  // mock.onGet('/test').networkError()
  mock.onGet('/test').reply(config => {
    const { startDate, endDate } = config.params
    if (startDate && endDate) {
      if (startDate < endDate) {
        return [200, generateRandomData(5, 1, 8)]
      }
      return [200, []]
    }
    return [200, [1, 2, 3, 4, 5]]
  })

  // GET /api/sales
  mock.onGet('/sales').reply(config => {
    const { startDate, endDate } = config.params
    if (startDate && endDate) {
      if (startDate < endDate) {
        return [200, generateRandomData(7, 5, 50)]
      }
      return [200, []]
    }
    return [200, [5, 20, 36, 10, 10, 20, 5]]
  })

  // GET /api/ads
  mock.onGet('/ads').reply(config => {
    const { startDate, endDate } = config.params
    if (startDate && endDate) {
      if (startDate < endDate) {
        return [200, {
          item1: generateRandomData(7, 200, 400),
          item2: generateRandomData(7, 100, 300),
          item3: generateRandomData(7, 150, 350)
        }]
      }
      return [200, {
        item1: [],
        item2: [],
        item3: []
      }]
    }
    return [200, {
      item1: [320, 332, 301, 334, 390, 330, 320],
      item2: [120, 182, 191, 234, 290, 330, 310],
      item3: [220, 182, 191, 234, 290, 330, 310]
    }]
  });

  // GET /api/trends
  mock.onGet('/trends').reply(config => {
    let { countryName, startDate, endDate } = config.params
    let data = {
      countryName: "咸安区",
      countryNames: ["咸安区", "嘉鱼县", "赤壁市", "崇阳县", "通山县", "通城县"],
      rowList: [
        ["尿酸升高", "0.00%", "0.00%", "0.37%", "0.76%", "0.08%"],
        ["甘油三脂升高", "0.00%", "0.00%", "0.00%", "0.94%", "0.19%"],
        ["低密度脂蛋白升高", "0.00%", "0.00%", "0.35%", "0.66%", "0.14%"],
        ["总胆固醇升高", "0.00%", "0.00%", "0.31%", "0.66%", "0.11%"],
        ["空腹血糖升高", "0.00%", "0.00%", "0.00%", "0.65%", "0.12%"],
        ["尿比重升高", "0.00%", "0.00%", "0.49%", "0.22%", "0.00%"]
      ],
      yearList: ["2021", "2022", "2023", "2024", "2025"]
    }
    if (startDate == endDate) {
      countryName = data.countryNames[Math.random() * (data.countryNames.length - 1) + 1 | 0];
      data.countryName = countryName
      data.rowList = [];
    }
    return [200, data]
  })
}